package com.kmxd.ams.controller.use;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.use.IUseAppraiseSvc;
import com.kmxd.ams.client.use.dto.UseAppraiseAddDTO;
import com.kmxd.ams.client.use.dto.UseAppraiseEditDTO;
import com.kmxd.ams.client.use.dto.UseAppraiseQueryDTO;
import com.kmxd.ams.client.use.vo.UseAppraiseEffectListVO;
import com.kmxd.ams.client.use.vo.UseAppraiseListVO;
import com.kmxd.ams.client.use.vo.UseAppraiseScoreListVO;
import com.kmxd.ams.client.use.vo.UseAppraiseVO;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 利用评价表 前端控制器
 *
 * @author nickbi
 * @since 2023-01-19
 */
@RestController
@Tag(name = "利用评价表")
@RequestMapping("/use/useAppraise")
@AllArgsConstructor
public class UseAppraiseCtl {

  private IUseAppraiseSvc useAppraiseService;

  /** 分页查询 */
  @Operation(summary = "分页查询")
  @GetMapping("/selectPage")
  @AutoLog(module = "利用评价表", title = "分页查询")
  public R<Page<UseAppraiseListVO>> selectPage(Page reqPage, UseAppraiseQueryDTO req) {
    return R.success(useAppraiseService.selectPage(reqPage, req));
  }

  /** 列表查询 */
  @Operation(summary = "列表查询")
  @GetMapping("/selectList")
  @AutoLog(module = "利用评价表", title = "列表查询")
  public R<List<UseAppraiseListVO>> selectList(UseAppraiseQueryDTO req) {
    return R.success(useAppraiseService.selectList(req));
  }

  /** 新增 */
  @Operation(summary = "新增")
  @PostMapping("/add")
  @AutoLog(module = "利用评价表", title = "新增")
  public R<Void> add(@Validated @RequestBody UseAppraiseAddDTO req) {
    useAppraiseService.add(req);
    return R.success();
  }

  /** 修改 */
  @Operation(summary = "修改")
  @PostMapping("/edit")
  @AutoLog(module = "利用评价表", title = "修改")
  public R<Void> edit(@Validated @RequestBody UseAppraiseEditDTO req) {
    useAppraiseService.edit(req);
    return R.success();
  }

  /** 删除 */
  @Operation(summary = "删除")
  @PostMapping("/remove")
  @AutoLog(module = "利用评价表", title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    useAppraiseService.remove(ids);
    return R.success();
  }

  /** 查看 */
  @Operation(summary = "查看")
  @GetMapping("/view")
  @AutoLog(module = "利用评价表", title = "查看")
  public R<UseAppraiseVO> view(
      @RequestParam(value = "id", required = false) Long id,
      @RequestParam(value = "workId", required = false) Long workId) {
    return R.success(useAppraiseService.view(id, workId));
  }
  /** 导出 */
  @Operation(summary = "导出")
  @GetMapping("/export")
  @AutoLog(module = "利用评价表", title = "导出")
  @SneakyThrows
  public void export(UseAppraiseQueryDTO req, HttpServletResponse response) {
    String filename = "利用评价表列表" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(UseAppraiseListVO.class)
        .sheet("利用评价表列表")
        .doWrite(useAppraiseService.selectList(req));
  }

  /** 分页查询 */
  @Operation(summary = "评分分页查询")
  @GetMapping("/selectScorePage")
  @AutoLog(module = "利用评价表", title = "分页查询")
  public R<Page<UseAppraiseScoreListVO>> selectScorePage(Page reqPage, UseAppraiseQueryDTO req) {
    return R.success(useAppraiseService.selectScorePage(reqPage, req));
  }

  /** 导出 */
  @Operation(summary = "服务评价导出")
  @GetMapping("/exportScore")
  @AutoLog(module = "利用评价表", title = "服务评价导出")
  @SneakyThrows
  public void exportScore(UseAppraiseQueryDTO req, HttpServletResponse response) {
    String filename = "服务评价" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(UseAppraiseScoreListVO.class)
        .sheet("服务评价")
        .doWrite(useAppraiseService.selectScoreList(req));
  }

  /** 分页查询 */
  @Operation(summary = "按类型评分分页查询")
  @GetMapping("/selectScoreWithTypePage")
  @AutoLog(module = "利用评价表", title = "按类型评分分页查询")
  public R<Page<Map<String, Object>>> selectScoreWithTypePage(
      Page reqPage, UseAppraiseQueryDTO req) {
    return R.success(useAppraiseService.selectScoreWithWorkTypePage(reqPage, req));
  }

  /** 分页查询 */
  @Operation(summary = "利用效果查询")
  @GetMapping("/selectEffectPage")
  @AutoLog(module = "利用评价表", title = "利用效果查询")
  public R<Page<UseAppraiseEffectListVO>> selectEffectPage(Page reqPage, UseAppraiseQueryDTO req) {
    return R.success(useAppraiseService.selectEffectPage(reqPage, req));
  }

  /** 导出 */
  @Operation(summary = "利用效果查询导出")
  @GetMapping("/exportEffect")
  @AutoLog(module = "利用评价表", title = "利用效果查询导出")
  @SneakyThrows
  public void exportEffect(UseAppraiseQueryDTO req, HttpServletResponse response) {
    String filename = "利用效果" + "_" + System.currentTimeMillis() + ".xlsx";
    response.setContentType("application/vnd.ms-excel");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
    response.setHeader(
        "Content-Disposition",
        "attachment;filename=" + URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
    EasyExcel.write(response.getOutputStream())
        .head(UseAppraiseEffectListVO.class)
        .sheet("利用效果")
        .doWrite(useAppraiseService.selectEffectList(req));
  }
}
